const path = require('path');
const fs = require('fs');
const chalk = require('chalk');
const { spawn } = require('child_process');

// 1. 获取要测试的目录
const testSrc = getTestSrc(process.argv[2]);
if (!testSrc) return;
console.log(chalk.green('测试目录：' + testSrc));

// 2. 执行代码
const args = ['./scripts/jest.js', testSrc, '--watch'];
const jest = spawn('node', args, {
    stdio: 'inherit'                                    // important
})
jest.on('close', code => {
    if (code === 1) {
      process.exit(1);
    }
});

function getTestSrc (inputSrc) {
    let testDir = inputSrc || './src';
    let newPath = path.resolve(__dirname, '..', testDir);

    try {
        const stat = fs.statSync(newPath);
        return newPath;
    } catch {
        console.log(chalk.red(newPath + ' 目录/文件不存在'))
    }
}
